package com.tac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tac.common.Exception.CosTomException;
import com.tac.common.result.Result;
import com.tac.common.util.imgUtil;
import com.tac.dao.SlideDao;
import com.tac.pojo.Slide;
import com.tac.service.SlideService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class SlideServiceImpl extends ServiceImpl<SlideDao, Slide> implements SlideService {

    @Value("${img.path}")
    private String imgpath;
    /**
     * 查看所有轮播图
     * @param page
     * @param pagesize
     * @return
     */
    public Result<Page<Slide>> selectAll(Long page,Long pagesize){
        LambdaQueryWrapper<Slide> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Slide::getType,Slide::getSort);
        Page<Slide> pageinfo = new Page<>(page,pagesize);
        this.page(pageinfo,queryWrapper);
        return Result.success(pageinfo,"success");
    }

    /**
     * 添加轮播图
     * @param slide
     * @return
     */
    public Result saveSlide(Slide slide)  {
        boolean save = this.save(slide);
        if(save){
           return Result.success(null,"success",201);
        }
        throw  new CosTomException("添加失败");
    }

    /**
     * 修改轮播图
     * @param slide
     * @return
     */
    public Result updateSlide(Slide slide){

        boolean b = this.updateById(slide);
        if(!b){
            throw  new CosTomException("修改失败");
        }
        return  Result.success(null,"success");
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    public Result deleteById(List<Long> ids){
        boolean b = this.removeByIds(ids);
        if(!b){
            throw  new CosTomException("删除失败");
        }
        return  Result.success(null,"success",204);
    }

    /**
     * 状态切换启用或停用
     * @param id
     * @return
     */
    public  Result updateStatus(Long id,Integer status){
        Slide slide = new Slide();
        slide.setId(id);
        slide.setStatus(status);
        boolean b = this.updateById(slide);
        if(!b){
            throw  new CosTomException("修改失败");
        }
        return  Result.success(null,"success");
    }

    /**
     * 查看首页轮播图
     * @return
     */
    public Result<List<Slide>> selectSlide(){
        LambdaQueryWrapper<Slide> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Slide::getType,"首页轮播");
        queryWrapper.orderByAsc(Slide::getSort);
        queryWrapper.eq(Slide::getStatus,1);
        List<Slide> list = this.list(queryWrapper);
        return Result.success(list,"success");
    }

    /**
     * 查看分类
     * @return
     */
    public Result<List<String>> selectType(){
        LambdaQueryWrapper<Slide> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.groupBy(Slide::getType);
        queryWrapper.select(Slide::getType);
        List<Slide> list = this.list(queryWrapper);
        ArrayList<String> arrayList = new ArrayList<>();
        for(Slide slide:list){
            String type = slide.getType();
            arrayList.add(type);
        }
        return Result.success(arrayList,"success");
    }
}
